/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpecPanel.java
 *
 * Created on 14.12.2010, 18:22:32
 */

package gui;

import manager.GUIManager;
import model.model.Speciality;
import model.viewmodel.SpecTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author parshin
 */
public class SpecPanel extends javax.swing.JPanel {

    private GUIManager manager;
    private SpecTableModel model;
    private SpecDialog dialogSpec;

    public SpecPanel(GUIManager manager) {
        initComponents();
        this.manager = manager;
        initTableActions();
        dialogSpec = new SpecDialog(manager.getRootFrame(), true, manager.getTaskManager(), manager.getSpecManager(), false);
        model = new SpecTableModel(manager.getSpecManager());
        tableSpec.setModel(model);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        bAddSpec = new javax.swing.JButton();
        bDelSpec = new javax.swing.JButton();
        bEditSpec = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableSpec = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        bAddSpec.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/addSmall.png"))); // NOI18N
        bAddSpec.setToolTipText("Добавить специализацию");
        bAddSpec.setBorderPainted(false);
        bAddSpec.setContentAreaFilled(false);
        bAddSpec.setDefaultCapable(false);
        bAddSpec.setFocusPainted(false);
        bAddSpec.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bAddSpec.setIconTextGap(0);
        bAddSpec.setMargin(new java.awt.Insets(0, 0, 0, 0));
        bAddSpec.setPreferredSize(new java.awt.Dimension(45, 45));
        bAddSpec.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/res/addBigPressed.png"))); // NOI18N
        bAddSpec.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/res/addBig.png"))); // NOI18N
        bAddSpec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddSpecActionPerformed(evt);
            }
        });
        jPanel1.add(bAddSpec);

        bDelSpec.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/removeSmall.png"))); // NOI18N
        bDelSpec.setToolTipText("Удалить специализацию");
        bDelSpec.setBorderPainted(false);
        bDelSpec.setContentAreaFilled(false);
        bDelSpec.setFocusPainted(false);
        bDelSpec.setMargin(new java.awt.Insets(0, 0, 0, 0));
        bDelSpec.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/res/removeBigPressed.png"))); // NOI18N
        bDelSpec.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/res/removeBig.png"))); // NOI18N
        bDelSpec.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/res/removeBigPressed.png"))); // NOI18N
        bDelSpec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bDelSpecActionPerformed(evt);
            }
        });
        jPanel1.add(bDelSpec);

        bEditSpec.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/editSmall.png"))); // NOI18N
        bEditSpec.setToolTipText("Редактировать специализацию");
        bEditSpec.setBorderPainted(false);
        bEditSpec.setContentAreaFilled(false);
        bEditSpec.setFocusPainted(false);
        bEditSpec.setMargin(new java.awt.Insets(0, 0, 0, 0));
        bEditSpec.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/res/editBigPressed.png"))); // NOI18N
        bEditSpec.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/res/editBig.png"))); // NOI18N
        bEditSpec.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/res/editBigPressed.png"))); // NOI18N
        bEditSpec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEditSpecActionPerformed(evt);
            }
        });
        jPanel1.add(bEditSpec);

        add(jPanel1, java.awt.BorderLayout.NORTH);

        tableSpec.setAutoCreateRowSorter(true);
        tableSpec.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null},
                        {null, null, null, null}
                },
                new String[]{
                        "Title 1", "Title 2", "Title 3", "Title 4"
                }
        ));
        jScrollPane1.setViewportView(tableSpec);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void initTableActions() {
        final JPopupMenu popMenu = new JPopupMenu();
        popMenu.add(new AbstractAction("Изменить специализацию") {
            public void actionPerformed(ActionEvent e) {
                editSelectedSpeciality();
            }
        });
        popMenu.add(new AbstractAction("Удалить специализацию") {
            public void actionPerformed(ActionEvent e) {
                deleteSelectedSpeciality();
            }
        });
        popMenu.add(new JSeparator());
        popMenu.add(new AbstractAction("Новая специализация") {
            public void actionPerformed(ActionEvent e) {
                addSpeciality();
            }
        });
        tableSpec.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
                    editSelectedSpeciality();
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                int i = tableSpec.rowAtPoint(new Point(e.getX(), e.getY()));
                if (i >= 0 && i < tableSpec.getRowCount())
                    tableSpec.setRowSelectionInterval(i, i);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                mousePressed(e);
                if (e.getButton() == MouseEvent.BUTTON3) {
                    if (tableSpec.contains(new Point(e.getX(), e.getY())))
                        popMenu.show(tableSpec, e.getX(), e.getY());
                }
            }
        });
    }

    private void bAddSpecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddSpecActionPerformed
        addSpeciality();
    }//GEN-LAST:event_bAddSpecActionPerformed

    private void bDelSpecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDelSpecActionPerformed
        deleteSelectedSpeciality();
    }//GEN-LAST:event_bDelSpecActionPerformed

    private void bEditSpecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEditSpecActionPerformed
        editSelectedSpeciality();
    }//GEN-LAST:event_bEditSpecActionPerformed

    public void addSpeciality() {
        if (dialogSpec.showDialog(null) == SpecDialog.OK) {
            manager.addSpeciality(dialogSpec.getSpeciality(), dialogSpec.getSpecQualifies());
        }
    }

    public void editSelectedSpeciality() {
        Speciality speciality = getSelectedSpeciality();
        if (speciality == null) return;
        if (dialogSpec.showDialog(speciality) == SpecDialog.OK) {
            manager.changeSpeciality(speciality, dialogSpec.getSpeciality(), dialogSpec.getSpecQualifies());
        }
    }

    public void deleteSelectedSpeciality() {
        Speciality speciality = getSelectedSpeciality();
        if (speciality == null) return;
        manager.removeSpeciality(speciality);
    }

    public Speciality getSelectedSpeciality() {
        int index = tableSpec.getSelectedRow();
        if (index >= 0) {
            index = tableSpec.convertRowIndexToModel(index);
            return model.getSpecialityAtRow(index);
        } else return null;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAddSpec;
    private javax.swing.JButton bDelSpec;
    private javax.swing.JButton bEditSpec;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableSpec;
    // End of variables declaration//GEN-END:variables

}
